% by ylx sby 6/11
% Version 5.0
% Based on SBY's fQR.m
% calculating profit
function [profit,EYI_new,EYin_new,ProfitPart_new] = function_fQR(Q,R,EYI,EYin,ProfitPart,n)
	global I ci cI r % 主程序中定义
	EYI_new=EYI;
    EYin_new=EYin;
    ProfitPart_new=ProfitPart;
    Q_new = Q;
	R_new = R;
	cols = size(Q, 2);
	for nn=1:cols
		Q_new(:, nn) = r * (nn - 1) * Q_new(:, nn);
		R_new(:, nn) = (nn - 1) * R_new(:, nn);
    end
        EYI_new(1, n)=sum(R_new(I, :));
        EYin_new(1, n)=sum(sum(R_new))/n;
		R_new = ci * R_new;
		R_new(I, :) = cI / ci * R_new(I, :);
        ProfitPart_new(1, n)=sum(sum(Q_new));
        ProfitPart_new(2, n)=ci*EYin_new(1, n)*n;%这就是作者错的地方，实际上不能用前面的EYin，应该单独算前I-1行。为了复现我这与原文保持一致
        ProfitPart_new(3, n)=sum(R_new(I, :));
	    profit = ProfitPart_new(1, n)-ProfitPart_new(2, n)-ProfitPart_new(3, n);     
return;


